package com.example.diyview.Slide;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.example.diyview.R;

public class SlideMenuView extends ViewGroup {
    private static final String TAG = "SlideMenuView";
    private View meditVIew;
    public SlideMenuView(Context context) {
        this(context,null);
    }

    public SlideMenuView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public SlideMenuView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.SlideMenuView);
        int funtion = a.getInt(R.styleable.SlideMenuView_function, 0x30);
        Log.d(TAG, "SlideMenuView:funtion"+funtion);
        int childCount = getChildCount();
        Log.d(TAG, "SlideMenuView: childCount"+childCount);
        a.recycle();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        int childCount = getChildCount();
        Log.d(TAG, "SlideMenuView: childCount"+childCount);
//        添加判断只能有一个子view
        if (childCount>1){
            throw  new IllegalArgumentException("超过了一个子view");
        }
//        根据属性继续添加子view
         meditVIew = LayoutInflater.from(getContext()).inflate(R.layout.item_slide_action, this, true);
        int afterinflater =   getChildCount();
        Log.d(TAG, "onFinishInflate: afterinflater"+afterinflater);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int heightsize = MeasureSpec.getSize(heightMeasureSpec);
        int widthsize= MeasureSpec.getSize(widthMeasureSpec);
        int childCount = getChildCount();
        Log.d(TAG, "onMeasure:childCount "+childCount);
        for (int i = 0; i < 1; i++) {
//            测量第一个孩子
            View childAt = getChildAt(i);
            int height = childAt.getLayoutParams().height;
            Log.d(TAG, "onMeasure:height "+height);
            int width = childAt.getLayoutParams().width;
            Log.d(TAG, "onMeasure: width"+width);
            int contentHeightMeasureSpec;
            if(height == LayoutParams.MATCH_PARENT){
                contentHeightMeasureSpec = MeasureSpec.makeMeasureSpec(heightsize,MeasureSpec.EXACTLY);
            }else if (height == LayoutParams.WRAP_CONTENT){
                contentHeightMeasureSpec = MeasureSpec.makeMeasureSpec(heightsize,MeasureSpec.AT_MOST);
            }else {
                contentHeightMeasureSpec = MeasureSpec.makeMeasureSpec(heightsize,MeasureSpec.EXACTLY);
            }
            childAt.measure(widthMeasureSpec,contentHeightMeasureSpec);
            int editWidthSize = widthsize*3/4;
            meditVIew.measure(MeasureSpec.makeMeasureSpec(editWidthSize,MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec(contentHeightMeasureSpec,MeasureSpec.EXACTLY));
//测量自己
            setMeasuredDimension(widthsize+editWidthSize,contentHeightMeasureSpec);
        }

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

    }
}
